//
//  MagicTower.m
//  TowerDefence
//
//  Created by  on 5/29/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MagicTower.h"
#import "PlayScreen.h"
#import "iCoreGUIController.h"

@implementation MagicTower

-(id)initMagicTowerWith:(GameLayer *)_gameLayer {
    self = [super initTowerObjectWith:_gameLayer];
    maxRadiusFireTarget = 150.0f;
    [self loadMagicTower];
    buyPrice = MAGIC_TOWER_CONSTRUCTION_PRICE;
    //range.scale=maxRadiusFireTarget*2/range.contentSize.width;
    return self;
}

-(void)loadMagicTower {
    temp = [[CCSprite alloc]initWithSpriteFrameName:@"magicTower_beforeShoot_01.png"];

    //[temp runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
    menuItemSpriteMagicTower = [CCMenuItemSprite itemFromNormalSprite:temp selectedSprite:nil target:self selector:@selector(showOption)];

    menuMagicTower = [CCMenu menuWithItems:menuItemSpriteMagicTower, nil];
    menuMagicTower.position = ccp(0,0);
    [spriteBody addChild:menuMagicTower];
    
}

-(void)fireTarget {
    if(!handler.mainGUIController.isMuteSound) {
        [handler.mainGUIController playEffect:AUDIO_SILVER_LIGHT];
    }
    
    if([handler.listGameObject containsObject:targetLocked]) {
        bullet = [[MagicRay alloc]initMagicRayWith:handler tower:self];
        bullet.spriteBody.position = ccp(spriteBody.position.x + 5, spriteBody.position.y + 20);
        bullet.spriteBody.anchorPoint = ccp(0.5, 0);
        
        CGPoint targetPos;
        if(((Enemy *)targetLocked).inBattle) {
            targetPos = targetLocked.spriteBody.position;
        } else {
            targetPos = [(Enemy *)targetLocked getPositionAfterTime:bullet.velocity];
        }
        targetPos = ccp(targetPos.x, targetPos.y + targetLocked.spriteBody.contentSize.height / 2 - 5);
        
        if(!CGPointEqualToPoint(targetPos, targetLocked.spriteBody.position) && [handler.listGameObject containsObject:targetLocked]) {
            [(PlayScreen *)handler fireEnemy:targetPos tower:self bullet:bullet];
        }
        
    } else {
        isShooting = NO;
    }
    
}

-(void)shootTargetAnimation {
    NSMutableArray *frameBeforeShootAnimationArray = [self loadAnimationWithNumberOfFrames:10 withFrameName:@"magicTower_beforeShoot"];    
    CCAnimation *beforeShootAnimation = [CCAnimation animationWithFrames:frameBeforeShootAnimationArray delay:0.075];
    
    NSMutableArray *frameAfterShootAnimation = [self loadAnimationWithNumberOfFrames:8 withFrameName:@"magicTower_afterShoot"];
    CCAnimation *afterShootAnimation = [CCAnimation animationWithFrames:frameAfterShootAnimation delay:0.075];
    
    CCCallFunc *fireEnemyCallback = [CCCallFunc actionWithTarget:self selector:@selector(fireTarget)];
    
    [temp runAction:[CCSequence actions:[CCAnimate actionWithAnimation:beforeShootAnimation restoreOriginalFrame:NO], fireEnemyCallback, [CCAnimate actionWithAnimation:afterShootAnimation restoreOriginalFrame:NO], nil]];
}

-(void)hitTargetOfBullet:(GameObject *)_bullet {
    //Degree HP of enemy
    [_bullet.spriteBody removeFromParentAndCleanup:YES];
    [handler removeGameObject:_bullet];
    
    if([((PlayScreen *)handler).listGameObject containsObject:targetLocked]){
        CCSprite *summonCircle=[CCSprite spriteWithSpriteFrameName:@"Circle_01.png"];
        summonCircle.position = ccp(targetLocked.spriteBody.contentSize.width/2, targetLocked.spriteBody.contentSize.height/2);
        summonCircle.scale = 0;
        //summonCircle.opacity = 90;
        
        [targetLocked.spriteBody addChild:summonCircle z:-1];
        
        NSMutableArray *anim = [[[GameObject alloc]initGameObjectWith:handler]loadAnimationWithNumberOfFrames:19 withFrameName:@"Circle"];
        CCAnimation *animation = [CCAnimation animationWithFrames:anim delay:0.05];
        [summonCircle runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.15 scale:0.5f], [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO],[CCScaleTo actionWithDuration:0.15 scale:0.0f], nil]];
        
        ((Enemy *) targetLocked).HP -= ((BulletObject*)_bullet).damage;
        [((Enemy *) targetLocked) updateBloodMeter];
    }
    
    isShooting = NO;
}

-(void)dealloc {    
    [temp stopAllActions];
    [temp removeAllChildrenWithCleanup:YES];
    [temp removeFromParentAndCleanup:YES];
    [temp release];
    
    [self stopAllActions];
    [self removeAllChildrenWithCleanup:YES];
    [self removeFromParentAndCleanup:YES];
    
    [self removeAnimationInFrameCache:@"magicTower_beforeShoot"];
    [self removeAnimationInFrameCache:@"magicTower_afterShoot"];
    
    [super dealloc];
}

@end
